
package com.yunfei.api.shuanglu;

import com.yunfei.api.user.contants.DoubleViedoSyncToCrmContants;
import com.yunfei.api.user.contants.UserCenterContants;
import com.yunfei.api.user.enums.ShuangLuResultStatus;
import com.yunfei.api.user.param.DoubleViedoAddParam;
import com.yunfei.api.user.param.DoubleViedoDeleteParam;
import com.yunfei.api.user.param.DoubleViedoGetParam;
import com.yunfei.api.user.param.SetHourseNoParam;
import com.yunfei.api.user.result.DoubleVideoIsUploadResult;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.RedisConstants;
import com.yunfei.entity.doubleViedoAcceDtl.DoubleViedoAcceDtlEntity;
import com.yunfei.enums.ResultStatus;
import com.yunfei.fegin.RedisFeignService;
import com.yunfei.mapper.aiiidb.DoubleViedoAcceDtlMapper;
import com.yunfei.mapper.aiiidb.GetStatusMapper;
import com.yunfei.mapper.aiiidb.TPersonalUserMapper;
import com.yunfei.mapper.crm.CrmShuangLuMapper;
import com.yunfei.ribbon.RedisServiceRibbon;
import com.yunfei.service.RedisService;
import com.yunfei.service.ValidMobileAndCodeService;
import com.yunfei.utils.http.CopyOfNgnixLiveUtil;
import com.yunfei.utils.id.IdUtils;
import com.yunfei.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 用户中心
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@Api(value = UserCenterContants.USER_CENTER_API_NAMESPACE,tags = UserCenterContants.USER_CENTER_API_NAMESPACE )
@RestController
@RequestMapping("/api/v2/shuanglu")
public class ShuangLuApiController {
    @Autowired
    private TPersonalUserMapper tPersonalUserMapper;
    @Autowired
    private GetStatusMapper getStatusMapper;
    @Autowired
    private RedisFeignService redisFeignService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private RedisServiceRibbon redisServiceRibbon;
    @Autowired
    private ValidMobileAndCodeService validMobileAndCodeService;
    @Autowired
    private Environment env;
    @Autowired
    private DoubleViedoAcceDtlMapper doubleViedoAcceDtlMapper;
    @Autowired
    private CrmShuangLuMapper crmShuangLuMapper;
    @Value("${HT_UPLOAD_DOUBLEVIDEO_TOCRM_API}")
    String htUploadDoubleVideoToCrmApi;
    /**
     * @Description: 撤销
     * @param reserveno 预约编号
     * @Author: zhengyunfei
     * @Date: 2019/1/11
     * @time:17:00
     */
    @PostMapping(value = "/add",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "新增双录文件",notes = UserCenterContants.AUTHOR_NOTE+"<br>新增双录文件",position = 1,response = Object.class)
    @ResponseBody
    public Object saveShuangLu(DoubleViedoAddParam param){
        ResultModel resultModel=new ResultModel();
        DoubleViedoAcceDtlEntity addParam=new DoubleViedoAcceDtlEntity();
        Map<String,Object> resultMap=new HashMap<>();
        try{
            String reserveId=param.getReserveId();//
            String fileName=param.getFileName();//双录视频名称预约编号
            //校验参数
            if(StringUtil.isNullOrEmpty(reserveId)){
                resultModel.setMessage(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getMessage());
                resultModel.setCode(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getCode());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(fileName)){
                resultModel.setMessage(ShuangLuResultStatus.FILENAME_NOTNULL.getMessage());
                resultModel.setCode(ShuangLuResultStatus.FILENAME_NOTNULL.getCode());
                return resultModel;
            }
            //通过双录视频名称，获取分布式视频文件访问地址
            //String strUTF8 = URLDecoder.decode(fileName, "UTF-8");
           // String doubleViedoUrl= CopyOfNgnixLiveUtil.getDoubleVideoUrl(fileName);
           // System.out.println("新增双录获取地址================================="+doubleViedoUrl);
            //封装双录视频新增参数
            String doubleViedoId= IdUtils.getId();
            addParam.setDoubleViedoId(doubleViedoId);
            //addParam.setDoubleViedoUrl(doubleViedoUrl);
            addParam.setReserveId(reserveId);
            addParam.setDoubleViedoName(fileName);
            doubleViedoAcceDtlMapper.add(addParam);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
    @PostMapping(value = "/update",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "编辑双录文件",notes = UserCenterContants.AUTHOR_NOTE+"<br>编辑双录文件",position = 1,response = Object.class)
    @ResponseBody
    public Object updateShuangLu(DoubleViedoAddParam param){
        ResultModel resultModel=new ResultModel();
        DoubleViedoAcceDtlEntity addParam=new DoubleViedoAcceDtlEntity();
        Map<String,Object> resultMap=new HashMap<>();
        String reserveId=param.getReserveId();//
        String fileName=param.getFileName();//双录视频名称预约编号
        String doubleViedoId= IdUtils.getId();
        try{
            //校验参数
            if(StringUtil.isNullOrEmpty(reserveId)){
                resultModel.setMessage(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getMessage());
                resultModel.setCode(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getCode());
                return resultModel;
            }
            if(StringUtil.isNullOrEmpty(fileName)){
                resultModel.setMessage(ShuangLuResultStatus.FILENAME_NOTNULL.getMessage());
                resultModel.setCode(ShuangLuResultStatus.FILENAME_NOTNULL.getCode());
                return resultModel;
            }
            Map<String,Object> queryMap=new HashMap<>();
            queryMap.put("reserveId",param.getReserveId());
            DoubleViedoAcceDtlEntity doubleViedoAcceDtlBo= (DoubleViedoAcceDtlEntity) doubleViedoAcceDtlMapper.get(queryMap);
            String dvId=doubleViedoAcceDtlBo.getDoubleViedoId();
            //删除智能投顾数据库附件
            Map<String,Object> deleteMap=new HashMap<>();
            deleteMap.put("doubleViedoId",dvId);
            doubleViedoAcceDtlMapper.delete(deleteMap);
            //删除crm数据库
            crmShuangLuMapper.deleteShuangLuFile(deleteMap);
            //String doubleViedoUrl= CopyOfNgnixLiveUtil.getDoubleVideoUrl(fileName);
            //编辑双录需要先将其他的双录视频EFFE_FLG设置为N
            DoubleViedoAcceDtlEntity doubleViedoAcceDtlEntity=new DoubleViedoAcceDtlEntity();
            doubleViedoAcceDtlEntity.setReserveId(reserveId);
            doubleViedoAcceDtlEntity.setEffeFlg("N");
            doubleViedoAcceDtlMapper.updateOtherEffeFlgEqualN(doubleViedoAcceDtlEntity);
            //通过双录视频名称，获取分布式视频文件访问地址
            System.out.println("1传递过来的参数fileName=================================="+fileName);
            //封装双录视频新增参数
            addParam.setDoubleViedoId(doubleViedoId);
           // addParam.setDoubleViedoUrl(doubleViedoUrl);
            addParam.setDoubleViedoName(fileName);
            addParam.setReserveId(reserveId);
            doubleViedoAcceDtlMapper.add(addParam);
            System.out.println("2传递过来的预约编号=========================="+reserveId);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData("保存成功");
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }finally {

        }
        return resultModel;
    }
    @PostMapping(value = "/get",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "获取双录文件",notes =UserCenterContants.AUTHOR_NOTE+"<br>获取双录文件",position = 1,response = Object.class)
    @ResponseBody
    public Object getShuangLuVideo(DoubleViedoGetParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            queryMap.put("reserveId",param.getReserveId());
            DoubleViedoAcceDtlEntity doubleViedoAcceDtlBo= (DoubleViedoAcceDtlEntity) doubleViedoAcceDtlMapper.get(queryMap);
            if(!StringUtil.isNullOrEmpty(doubleViedoAcceDtlBo)){
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                String doubleViedoUrl=doubleViedoAcceDtlBo.getDoubleViedoUrl();
                if(StringUtil.isNullOrEmpty(doubleViedoUrl)){
                    String fileName=doubleViedoAcceDtlBo.getDoubleViedoName();
                    System.out.println("fileName=============================="+fileName);
                    String strUTF8 = URLDecoder.decode(fileName, "UTF-8");
                    doubleViedoUrl= CopyOfNgnixLiveUtil.getDoubleVideoUrl(strUTF8);
                    System.out.println("获取双录获取地址================================="+doubleViedoUrl);
                    doubleViedoAcceDtlBo.setDoubleViedoUrl(doubleViedoUrl);
                    DoubleViedoAcceDtlEntity updateParam=new DoubleViedoAcceDtlEntity();
                    updateParam.setDoubleViedoUrl(doubleViedoUrl);
                    updateParam.setDoubleViedoId(doubleViedoAcceDtlBo.getDoubleViedoId());
                    doubleViedoAcceDtlMapper.update(updateParam);
                }
                resultModel.setData(doubleViedoAcceDtlBo);
            }else{
                resultModel.setCode(ResultStatus.NO_DATA.getCode());
                resultModel.setMessage(ResultStatus.NO_DATA.getMessage());
                resultModel.setData(DoubleViedoSyncToCrmContants.NOT_DATE_MESSAGE);
            }

        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
    /**
    * @Description: 删除双录文件
    * @Param: [param]
    * @return: java.lang.Object
    * @Author: zhengyunfei
    * @Date: 2019/6/21
    * @time:14:38
    */
    @PostMapping(value = "/delete",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除双录文件",notes = UserCenterContants.AUTHOR_NOTE+"<br>删除双录文件",position = 1,response = Object.class)
    @ResponseBody
    public Object delete(DoubleViedoDeleteParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String doubleViedoId=param.getDoubleViedoId();
            queryMap.put("doubleViedoId",doubleViedoId);
            //将有效状态更改为N
            doubleViedoAcceDtlMapper.updateFlg(queryMap);
            //删除智能投顾库的crmAcceDtl中的数据
            doubleViedoAcceDtlMapper.delete(queryMap);
            //同时删除crm库
            crmShuangLuMapper.deleteShuangLuFile(queryMap);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }


    @PostMapping(value = "/isUpload",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "更改合同提交",notes =  UserCenterContants.AUTHOR_NOTE+"<br>在合同提交时，需要调用此接口，如果返回结果isUpload大于0，代表有双录视频并且双录视频未上传,还需调用CRM同步逻辑的接口，接口地址，参考本接口返回的字段url，接口文档地址参考本接口返回的apiDocument",position = 1,response = DoubleVideoIsUploadResult.class)
    @ResponseBody
    public Object crmSync(DoubleViedoGetParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveId=param.getReserveId();//
            queryMap.put("reserveId",reserveId);
            //校验参数
            if(StringUtil.isNullOrEmpty(reserveId)){
                resultModel.setMessage(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getMessage());
                resultModel.setCode(ShuangLuResultStatus.RESERVE_ID_NOTNULL.getCode());
                return resultModel;
            }
            DoubleVideoIsUploadResult doubleVideoIsUploadResult=new DoubleVideoIsUploadResult();
            int count=doubleViedoAcceDtlMapper.isUploadDoubleViedo(queryMap);
            doubleVideoIsUploadResult.setIsUpload(count);
            if(count>0){
                doubleVideoIsUploadResult.setUrl(htUploadDoubleVideoToCrmApi+ DoubleViedoSyncToCrmContants.SYNC_TOCRM_API);
                doubleVideoIsUploadResult.setApiDocument(htUploadDoubleVideoToCrmApi+ DoubleViedoSyncToCrmContants.API_DOCMENT);
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            resultModel.setData(doubleVideoIsUploadResult);
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }

    @PostMapping(value = "/setHouseNoKey",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "将房间号保存到缓存中",notes ="将房间号保存到缓存中",position = 1,response = Object.class)
    @ResponseBody
    public Object setHouseNoKey(SetHourseNoParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveId=param.getReserveId();
            String hourseNo=param.getHourseNo();
            redisService.set(reserveId,hourseNo, RedisConstants.SESSION_EXPIRES_1, TimeUnit.HOURS);
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
    @PostMapping(value = "/getHouseNoKey",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "根据预约编号获取房间号",notes ="根据预约编号获取房间号",position = 1,response = Object.class)
    @ResponseBody
    public Object getHouseNoKey(DoubleViedoGetParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveId=param.getReserveId();
            if(redisService.exists(reserveId)){
                Object data=redisService.get(reserveId);
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                resultModel.setData(data);
            }else{
                resultModel.setCode(ResultStatus.HOURSE_NO_NOTEXSIT.getCode());
                resultModel.setMessage(ResultStatus.HOURSE_NO_NOTEXSIT.getMessage());
            }

        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
    @PostMapping(value = "/removeHouseNoKey",produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "根据预约编号获取房间号",notes ="根据预约编号获取房间号",position = 1,response = Object.class)
    @ResponseBody
    public Object removeHouseNoKey(DoubleViedoGetParam param){
        ResultModel resultModel=new ResultModel();
        Map<String,Object> resultMap=new HashMap<>();
        Map<String,Object> queryMap=new HashMap<>();
        try{
            String reserveId=param.getReserveId();
            if(redisService.exists(reserveId)){
                Object data=redisService.get(reserveId);
                redisService.remove(reserveId);
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                resultModel.setData("预约编号为"+reserveId+"的房间号"+data+"已被移除");
            }else{
                resultModel.setCode(ResultStatus.HOURSE_NO_NOTEXSIT.getCode());
                resultModel.setMessage(ResultStatus.HOURSE_NO_NOTEXSIT.getMessage());
            }

        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            resultModel.setData(null);
        }
        return resultModel;
    }
}
